package com.devunion.bogdan.view;

import com.devunion.bogdan.view.util.DialogHelper;

import javax.swing.*;
import javax.swing.border.LineBorder;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

/**
 * @author Viktar Liaskovich
 */
public class StartPanel extends JPanel {
    private int itemsInLeftColumn;
    private int itemsInRightColumn;

    public StartPanel() {
        super(new GridBagLayout());
        setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
        setBackground(Color.WHITE);
    }

    public void addActionToTheLeftColumn(String title, String description, Action action) {
        DialogHelper.placeComponent(this, new ActionPanel(title, description, action),
                0, itemsInLeftColumn, 1, 1,
                1.0, 0.0, GridBagConstraints.NORTHWEST, GridBagConstraints.BOTH,
                10, 10, 10, 10);

        itemsInLeftColumn++;
    }

    public void addActionToTheRightColumn(String title, String description, Action action) {
        DialogHelper.placeComponent(this, new ActionPanel(title, description, action),
                1, itemsInRightColumn, 1, 1,
                1.0, 0.0, GridBagConstraints.NORTHWEST, GridBagConstraints.BOTH,
                10, 10, 10, 10);

        itemsInRightColumn++;
    }

    public void pack() {
        DialogHelper.placeComponent(this, Box.createVerticalGlue(),
                0, Math.max(itemsInLeftColumn, itemsInRightColumn) + 1, 2, 1,
                1.0, 1.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH,
                0, 0, 0, 0);
    }

    private static class ActionPanel extends JPanel {
        private JLabel jLabelTitle;
        private JLabel jLabelDescription;
        private JButton button;

        private static Color DEFAULT_BG_COLOR = new Color(210, 213, 226);

        // TODO: Use gradient instead of BG
        public ActionPanel(String title, String description, Action action) {
            super(new GridBagLayout());
            setBackground(DEFAULT_BG_COLOR);

            createTitleLabel(title);
            createDescriptionLabel(description);
            createButton(action);

            init();
        }

        private void createButton(Action action) {
            button = new PanelButton(action);
        }

        private void init() {
            DialogHelper.placeComponent(this, button, 0, 0, 1, 3,
                    0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.BOTH,
                    0, 0, 0, 0);

            DialogHelper.placeComponent(this, jLabelTitle, 1, 0, 1, 1,
                    1.0, 0.0, GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL,
                    2, 16, 0, 0);

            DialogHelper.placeComponent(this, jLabelDescription, 1, 1, 1, 1,
                    0.0, 0.0, GridBagConstraints.NORTH, GridBagConstraints.BOTH,
                    4, 16, 0, 0);

            DialogHelper.placeComponent(this, Box.createVerticalGlue(), 1, 2, 1, 1,
                    0.0, 1.0, GridBagConstraints.CENTER, GridBagConstraints.VERTICAL,
                    0, 0, 0, 0);
        }

        private void createDescriptionLabel(String description) {
            jLabelDescription = new JLabel(description);
            jLabelDescription.setFont(new Font("Arial", Font.PLAIN, 12));
        }

        private void createTitleLabel(String title) {
            jLabelTitle = new JLabel(title);
            jLabelTitle.setFont(new Font("Arial", Font.BOLD, 13));
        }
    }

    private static class PanelButton extends JButton {
        private static LineBorder DEFAULT_BORDER = new LineBorder(Color.BLACK, 1, true);
        private static Dimension DEFAULT_PREFERRED_SIZE = new Dimension(72, 72);
        private static Color DEFAULT_FG_COLOR = new Color(201, 205, 217);
        private static Color HIGHLIGHTED_FG_COLOR = new Color(187, 187, 255);

        public PanelButton(Action action) {
            super(action);

            setPreferredSize(DEFAULT_PREFERRED_SIZE);
            setFocusable(false);
            setBorder(DEFAULT_BORDER);
            setBackground(DEFAULT_FG_COLOR);

            setRolloverEnabled(true);

            addMouseListener(new MouseAdapter() {
                public void mouseEntered(MouseEvent e) {
                    setBackground(HIGHLIGHTED_FG_COLOR);
                }

                public void mouseExited(MouseEvent e) {
                    setBackground(DEFAULT_FG_COLOR);
                }
            });
        }
    }
}
